<?php

namespace MoChat\App\Call\Action\Dashboard\Plan;

use MoChat\App\Call\Service\CallPlanService;
use MoChat\App\Sales\Contract\SalesTaskContract;
use MoChat\Framework\Action\AbstractAction;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Request\ValidateSceneTrait;

/**
 * 呼叫计划列表
 * @Controller
 */
class Index extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var CallPlanService
     */
    protected $callPlanService;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class),
     * })
     * @RequestMapping(path="/dashboard/callPlan/list", methods="get")
     */
    public function handle()
    {
        $user = user();
        // 判断用户绑定企业信息
        if (!isset($user['corpIds']) || count($user['corpIds']) !== 1) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '未选择登录企业，不可操作');
        }
        $data = $this->request->all();
        $this->validated($data);

        $where = [];
        if (isset($data['name']) && !empty($data['nane'])) {
            $where[] = ['name', 'like', '%' . $data['name'] . '%'];
        }
        if (!empty($data['startTime'])) {
            $where[] = ['created_at', '>=', $data['startTime']];
            $where[] = ['created_at', '<', $data['endTime']];
        }

     return $this->callPlanService->getIndexContract($where);
    }

    /**
     * 验证规则.
     */
    protected function rules(): array
    {
        return [
            'name' => 'max:30',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    protected function messages(): array
    {
        return [
            'name.max' => '计划名称长度不能超过30个字符',
        ];
    }
}